package org.study;

import org.study.module.jdk.JdkShell;
import org.study.module.mysql.MysqlShell;
import org.study.module.common.ShellPre;
import org.study.shell.ShellRun;
import org.study.utils.FileUtils;

import java.util.List;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {

    private static String softPath = "/opt/softwar";
    private static String install = "/opt/module";
    private static String localShell = "D:\\Java\\hadoop-install\\shell";
    private static String username = "bdc";
    private static String password = "123123";

    private static String host = "192.168.56.22";

    public static void main(String[] args) {
        // Press Opt+Enter with your caret at the highlighted text to see how
        // IntelliJ IDEA suggests fixing it.
        System.out.printf("Hello and welcome!");
//        pre();
//        test();
        mysql();
//        jdk();


    }

    private static void test() {
        try {
            ShellRun run = new ShellRun(username, password, host);
            run.executeCommand("ls -l");
            run.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void pre() {
        try {
            ShellRun run = new ShellRun("root", password, host);
            FileUtils.write(ShellPre.preInstallByWheel(username, password, softPath, install), localShell, "pre_install.sh");
            run.uploadBySftp(localShell + "\\pre_install.sh", "/root");
            run.executeCommand("sh /root/pre_install.sh");
            run.executeCommand("ls -l");
            run.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void mysql() {
        try (ShellRun run = new ShellRun(username, password, host)) {
            List<String> commands = MysqlShell.preInstall(softPath);
            commands.addAll(MysqlShell.install(softPath, install, password));
//            FileUtils.write(commands, localShell, "mysql_install.sh");
//            FileUtils.write(MysqlShell.changePassword(password), localShell, "mysql_change.sh");
//            run.uploadBySftp("F:\\BaiduNetdiskDownload\\mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz", "/opt/softwar");
//            run.uploadBySftp("F:\\BaiduNetdiskDownload\\libaio-0.3.109-13.el7.x86_64.rpm", "/opt/softwar");
//            run.uploadBySftp("F:\\BaiduNetdiskDownload\\numactl-2.0.12-5.el7.x86_64.rpm", "/opt/softwar");
//            run.uploadBySftp("F:\\BaiduNetdiskDownload\\numactl-libs-2.0.12-5.el7.x86_64.rpm", "/opt/softwar");
//            run.uploadBySftp(localShell + "\\mysql_install.sh", "/opt/softwar/shell");
            run.uploadBySftp(localShell + "\\mysql_change.sh", "/opt/softwar/shell");
//            String info = run.executeCommand("sudo sh /opt/softwar/shell/mysql_install.sh");
            String info = run.executeCommand("sudo sh /opt/softwar/shell/mysql_change.sh");
            run.close();
            System.out.println(info);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void jdk() {
        try {
            ShellRun run = new ShellRun(username, password, host);
            FileUtils.write(JdkShell.install(softPath, install), localShell, "jdk_install.sh");
            run.uploadBySftp("F:\\BaiduNetdiskDownload\\jdk-8u241-linux-x64.tar.gz", "/opt/softwar");
            run.uploadBySftp(localShell + "\\jdk_install.sh", "/opt/softwar/shell");
            String info = run.executeCommand("ls -l /opt/softwar/shell");
            run.close();
            System.out.println(info);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}